package main;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import algorithm.Combination;

public class MatrixGeneration2 extends BaseGeneration {

	public static final String TERM_COUNT_FILE_NAME = "data/term-count-of-txts";

	public static final String OUTPUT_FILE_NAME = "data/matrix-generation-topic-matrix";

	public void gen() {
		try {
			readTermCount(TERM_COUNT_FILE_NAME);
			BufferedWriter bw = new BufferedWriter(new FileWriter(OUTPUT_FILE_NAME));
			Set<Entry<String, Topic>> set = topics.entrySet();
			List<Entry<String, Topic>> list = new ArrayList<Entry<String, Topic>>(set);
			Collections.sort(list, new comEntry());
			for (Iterator<Entry<String, Topic>> iter = list.iterator(); iter.hasNext();) {
				Topic topic = iter.next().getValue();
				genData(topic);
				write(topic, bw);
			}
			bw.close();
			// writeTermsCount();
			// genData();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private int getMaxInt(int count) {
		return count % 1000 == 0 ? count / 1000 : count / 1000 + 1;
	}

	private void genData(Topic topic) {
		Collections.sort(topic.getFullTerms());
		int cMax = getMaxInt(topic.getTotalCount());
		for (int i = 1; i <= cMax; i++) {
			Combination c = new Combination(topic.getFullTerms().toArray(new String[0]), i);
			c.doCombination();
			List<Object[]> cList = c.getCombination();
			List<boolean[]> kwList = new ArrayList<boolean[]>();
			for (Iterator<Passage> psgs = topic.getPassages().iterator(); psgs.hasNext();) {
				boolean[] idx = new boolean[cList.size()];
				Passage psg = psgs.next();
				for (int j = 0; j < idx.length; j++) {
					if (isContains(psg.getFoundTerms(), cList.get(j))) {
						idx[j] = true;
					}
				}
				kwList.add(idx);
			}
			topic.getOutput().add(cList);
			topic.getOutput().add(kwList);
		}
		// }
	}

	private void writeTermsCount() {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(OUTPUT_FILE_NAME));
			Set<Entry<String, Topic>> set = topics.entrySet();
			List<Entry<String, Topic>> list = new ArrayList<Entry<String, Topic>>(set);
			Collections.sort(list, new comEntry());
			for (Iterator<Entry<String, Topic>> iter = list.iterator(); iter.hasNext();) {
				Topic topic = iter.next().getValue();
				for (Iterator<Passage> itp = topic.getPassages().iterator(); itp.hasNext();) {
					Passage psg = itp.next();
					bw.write(psg.getTopicID() + "," + psg.getPassageID() + "," + psg.getTermsCount() + ","
							+ psg.getTotalTermsCount() + "," + psg.getTerms() + "\r\n");
				}
			}
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void write(Topic topic, BufferedWriter bw) {
		try {
			// BufferedWriter bw = new BufferedWriter(new
			// FileWriter(OUTPUT_FILE_NAME));
			int k = 1;
			int cMax = getMaxInt(topic.getTotalCount());
			bw.write("Topic #" + topic.getId() + " all terms:" + StringUtils.join(topic.getFullTerms(), ",") + "\r\n");
			for (int i = 0; i < topic.getOutput().size(); i++) {
				List listO = (List) topic.getOutput().get(i);
				if (listO.get(0) instanceof Object[]) {
					bw.write("keywords-" + k + "/" + cMax + "\r\n");
					StringBuffer out = new StringBuffer();
					for (Iterator<Object[]> iterB = listO.iterator(); iterB.hasNext();) {
						Object[] title = (Object[]) iterB.next();
						out.append(StringUtils.join(title, " ") + ",");
					}
					if (out.toString().endsWith(",")) {
						out.setLength(out.length() - 1);
					}
					bw.write(out.toString() + "\r\n");

				} else {
					for (Iterator<boolean[]> iterB = listO.iterator(); iterB.hasNext();) {
						boolean[] idx = iterB.next();
						StringBuffer out = new StringBuffer();
						for (int j = 0; j < idx.length; j++) {
							out.append(idx[j] ? "1" : "0").append(",");
						}
						out.setLength(out.length() - 1);
						bw.write(out.toString() + "\r\n");
					}
					k++;
				}

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		MatrixGeneration2 mg = new MatrixGeneration2();
		mg.gen();
	}
}
